<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false">
    <el-form ref="dataForm" :rules="rules" :model="temp" label-position="left" label-width="120px"
      style="width: 80%; margin-left:50px;">
      <el-row :gutter="40">

        <el-col :span="24">
          <el-form-item label="映射名称" prop="tableName">
            <el-input v-model="temp.tableName" />
          </el-form-item>
        </el-col>


        <el-col :span="24">
          <el-form-item label="所属数据源名称" prop="sourceName">
            <el-select v-model="temp.sourceId" class="filter-item" placeholder="请选择所属数据源名称" @change="choseBelongs">
              <el-option v-for="(item, index) in sourceData" :key="index" :label="item.sourceName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="来源表表名" prop="pTablename">
            <el-select v-model="temp.pTablename" class="filter-item" placeholder="请选择下级表的表名" :disabled="!temp.sourceId">
              <el-option v-for="(item, index) in tableData" :key="index" :label="item.tableName" :value="item.tableName" />
            </el-select>
            <!-- <el-cascader
              v-model="temp.pTablename"
              :options="treeList"
              :props="treeProps"
              :show-all-levels="false"
              label="name"
              change-on-select
              clearable
              style="width: 100%"
            /> -->
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="目标数据源名称" prop="targetSourcename">
            <el-select v-model="temp.targetSourceId" class="filter-item" placeholder="请选择目标数据源名称" @change="choseTarget">
              <el-option v-for="(item, index) in sourceData" :key="index" :label="item.sourceName" :value="item.id" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="目标表表名" prop="sonTablename">
            <el-select v-model="temp.sonTablename" class="filter-item" placeholder="请选择下级表的表名"
              :disabled="!temp.targetSourceId">
              <el-option v-for="(item, index) in targetData" :key="index" :label="item.tableName"
                :value="item.tableName" />
            </el-select>
            <!-- <el-cascader
              v-model="temp.sonTablename"
              :options="treeList"
              :props="treeProps"
              :show-all-levels="false"
              label="name"
              change-on-select
              clearable
              style="width: 100%"
            /> -->
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">
        {{ $t('table.cancel') }}
      </el-button>
      <el-button type="primary" :loading="loading" @click="dialogStatus === 'create' ? createData() : updateData()">
        {{ $t('table.confirm') }}
      </el-button>
    </div>
    <!-- <div class="el-message el-message--error"><p>请填写完整</p></div> -->
  </el-dialog>
</template>

<script>
import { createconsanguinity, updateconsanguinity, getconsanguinity } from '@/api/demo/consanguinity'
import { getTableList } from '@/api/demo/metadataTable'
import { getSourceList } from '@/api/demo/metadataSource'

export default {
  name: 'consanguinityForm',
  data() {
    return {
      rules: {},
      temp: {},
      textMap: {
        update: '编辑',
        create: '新建',
      },
      loading: false,
      dialogFormVisible: false,
      dialogStatus: '',
      treeList: [],
      treeProps: {
        value: 'id',
        label: 'name',
        expandTrigger: 'hover',
      },
      tableData: [],
      targetData: [],
      sourceData: [],
    }
  },
  created() {
    getSourceList().then((res) => {
      if (res.data.code === 200) {
        this.sourceData = res.data.data
        console.log(this.sourceData)
      }
    })
  },
  methods: {
    tableList() {
      getTableList(data).then(res => {
        if (res.code === 200) {
          this.tableData = res.data
          console.log(this.tableData)
        }
      })
    },
    resetTemp() {
      this.loading = false
      this.temp = {
        id: undefined,
        createBy: undefined,
        createDate: undefined,
        updateBy: undefined,
        updateDate: undefined,
        delFlag: undefined,
        parentId: undefined,
        sonId: undefined,
        convertId: undefined,
        sonTablename: undefined,
        pTablename: undefined,
        tableName: undefined,
        sourceId: undefined,
        sourceName: undefined,
        targetSourceId: undefined,
        targetSourcename: undefined,
      }
    },
    handleCreate() {
      this.resetTemp()
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
    },
    createData() {
      if (this.temp.tableName && this.temp.sourceName && this.temp.pTablename && this.temp.targetSourcename && this.temp.sonTablename) {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            createconsanguinity(this.temp).then((response) => {
              this.loading = false
              if (response.data.code === 0) {
                this.dialogFormVisible = false
                this.$message.success(response.data.msg)
                this.$parent.getList()
                this.tableList()
              } else {
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      } else {
        this.$message.error('请填写完整')
      }
    },
    setList(treeList) {
      this.treeList = treeList
    },
    //选择所属数据源
    choseBelongs(id) {
      this.temp.pTablename = ''
      let msg = this.sourceData.find(item => {
        return item.id == id
      })
      this.temp.sourceName = msg.sourceName
      getTableList({ sourceId: id }).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.data
        }
      })
    },
    //选择目标数据源
    choseTarget(id) {
      this.temp.sonTablename = ''
      let msg = this.sourceData.find(item => {
        return item.id == id
      })
      this.temp.targetSourcename = msg.sourceName
      getTableList({ sourceId: id }).then(res => {
        if (res.data.code === 200) {
          this.targetData = res.data.data
        }
      })
    },
    handleUpdate(id) {
      this.resetTemp()
      this.dialogStatus = 'update'
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].clearValidate()
      })
      getconsanguinity(id).then((response) => {
        if (response.data.code === 0) {
          this.temp = response.data.data
          getTableList({ sourceId: this.temp.targetSourceId }).then(res => {
            if (res.data.code === 200) {
              this.targetData = res.data.data
            }
          })
          getTableList({ sourceId: this.temp.sourceId }).then(res => {
            if (res.data.code === 200) {
              this.tableData = res.data.data
            }
          })
        } else {
          this.dialogFormVisible = false
          this.$message.error(response.data.msg)
        }
      })
    },
    updateData() {
      if (this.temp.tableName && this.temp.sourceName && this.temp.pTablename && this.temp.targetSourcename && this.temp.sonTablename) {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            updateconsanguinity(this.temp).then((response) => {
              this.loading = false
              if (response.data.code === 0) {
                this.dialogFormVisible = false
                this.$parent.getList()
                this.tableList()
                this.$message.success(response.data.msg)
              } else {
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      } else {
        this.$message.error('请填写完整')
      }

    },
  },
}
</script>